<?php
/**********************************************************************
 *                                                                    *    
 * Copyright (c) 2009 Dmytro Bezhnar. All rights reserved.     *
 *                                                                    *
 **********************************************************************/

/**
 * Ageset entity manager.
 *
 * @version $
 */
class StatisticsComoonManager extends ORMManager
{	
    private $project;
	private $db_name;

	private function setProjectId($id=1)
	{		 
		$this->project = $id*1;		
	}
	
	private function getProjectId()
	{
		return $this->project;
	}
	
	private function setDbName($db='statistics')
	{
		$this->db_name = $db;		
	}
	
	private function getDbName()
	{
		return $this->db_name;
	}
	
	function Init($project_id, $db)
	{
		$this->setProjectId($project_id);
		$this->setDbName($db);
	}
	
	function getHostData($obj_type_id, $obj_id, $host)
	{
		switch ($_SESSION['stat_period'])
		{
			case 1:
				$where_date = ' and u.time >= curdate()';
			break;
			case 2:
				$where_date = ' and u.time between DATE_SUB(CURDATE(),INTERVAL 1 DAY) and now()';
			break;
			case 3:
				$where_date = ' and u.time between DATE_SUB(CURDATE(),INTERVAL 7 DAY) and now()';
			break;
			case 4:
				$where_date = ' and u.time between DATE_SUB(CURDATE(),INTERVAL 30 DAY) and now()';
			break;
			default:
				$where_date = '';
			break;
		}
		
		$qs = "SELECT 
					s.HTTP_REFERER http_referer, count(s.HTTP_REFERER) cnt
				FROM 
					statistics.tmp_users u, 
					statistics.tmp_server s 
				where  
					HTTP_REFERER_HOST='$host' and 
					s.user_id=u.user_id and 
					u.project_id=1 and 
					u.obj_type_id=$obj_type_id and 
					u.obj_id=$obj_id 
					$where_date 
				group by 
					s.HTTP_REFERER 
				order by 
					cnt desc 
				limit 20 ";
		return $this->getAssArray($qs,'http_referer','cnt');
	}
	
	function getObjectData($obj_type_id, $obj_id)
	{
		
		switch ($_SESSION['stat_period'])
		{
			case 1:
				$where_date = ' and u.time >= curdate()';
			break;
			case 2:
				$where_date = ' and u.time between DATE_SUB(CURDATE(),INTERVAL 1 DAY) and now()';
			break;
			case 3:
				$where_date = ' and u.time between DATE_SUB(CURDATE(),INTERVAL 7 DAY) and now()';
			break;
			case 4:
				$where_date = ' and u.time between DATE_SUB(CURDATE(),INTERVAL 30 DAY) and now()';
			break;
			default:
				$where_date = '';
			break;
		}
		
		
		
		$qs = "SELECT 
						count(*) as cnt 
					FROM 
						statistics.tmp_users u, 
						statistics.tmp_session s
					WHERE 
						u.user_id=s.user_id and 
						u.obj_type_id=$obj_type_id and 
						u.obj_id=$obj_id 
						$where_date";
		$result = $this->getOneArray($qs);
		
		$hits = $result['cnt'];
		
		$qs = "SELECT 
					sql_calc_found_rows count(u.obj_id) as cnt 
				FROM 
					statistics.tmp_users u, 
					statistics.tmp_session s
				WHERE 
					u.user_id=s.user_id and 
					u.obj_type_id=$obj_type_id and 
					u.obj_id=$obj_id 
					$where_date
				group by 
					u.cookie 
				limit 1";
		$result = $this->getArray($qs);
		
		$qs='SELECT FOUND_ROWS() cnt';
		$result = $this->getOneArray($qs);
		
		$hosts = $result['cnt'];
		
		$qs = "SELECT sql_calc_found_rows
					s.http_referer_host, count(u.obj_id) cnt 
				FROM 
					statistics.tmp_users u,
					statistics.tmp_server s
				where 
					u.user_id=s.user_id and 
					u.obj_type_id=$obj_type_id and 
					u.obj_id=$obj_id and 
					s.HTTP_REFERER_HOST <> '' 
					$where_date
				GROUP BY		
					s.HTTP_REFERER_HOST 
				order by 
					cnt desc 
				LIMIT 20";
		$hosts_array = $this->getAssArray($qs,'http_referer_host','cnt');
		
		
		
		$qs='SELECT FOUND_ROWS() cnt';
		$result = $this->getOneArray($qs);
				
		
		return array('hosts'=>$hosts, 'hits' => $hits, 'hosts_count'=>$result['cnt'], 'hosts_array' => $hosts_array);
	}
	
	function InsertNewData($obj_type_id, $obj_id)
	{
		$is_bot=0;
		$unixtime = time();
		$time = date("Y-m-d H:i:s",$unixtime);
		$stat_cookie = '';
		if (isset($_COOKIE['stc']))
		{
			$cookie = $this->checkTextForMysql($_COOKIE['stc']);
			$stat_cookie = $cookie;
		}else 
		{			
			$cookie_value = md5($unixtime.'_'.mt_rand());
			if (setcookie('stc',$cookie_value,$unixtime+86400,'/'))
			{
				$cookie = $cookie_value;
			}else 
			{
				$cookie='';
			}
		}
		
		$user_state_id = 0;
		$user_obl_id = 0;
		$user_city_id = 0;
		if (isset($_COOKIE['CCC']))
		{
			list($user_state_id,$user_obl_id,$user_city_id) = spliti(':',$_COOKIE['CCC']);
			$user_state_id*=1;
			$user_obl_id*=1;
			$user_city_id*=1;
		}
		
		$lang_code = '';
		if (isset($_COOKIE['lang_code']))
		{
			$lang_code = $this->checkTextForMysql($_COOKIE['lang_code']);
		}
		
		$ip = REMOTE_ADDR;
		$city_id = IP_CITY_ID*1;
		
		$qs = "INSERT 
						".$this->getDbName().".tmp_users 
					SET 
						project_id=".$this->getProjectId().", 
						obj_type_id=$obj_type_id,
						obj_id=$obj_id,
						cookie='$cookie',
						ip=$ip,
						time='$time',
						unixtime=$unixtime,
						ria_user_id=". $_SESSION['person_id']*1 .",
						ip_city_id=$city_id,
						user_city_id=$user_city_id,
						is_bot=$is_bot";
		$this->execQuery($qs);
		
		$user_id = $this->getLastInsert();
		
		
		$qs = "INSERT DELAYED 
						".$this->getDbName().".tmp_cookie
					SET 
						user_id=$user_id,
						stat_cookie='$stat_cookie',
						user_state_id=$user_state_id,
						user_obl_id=$user_obl_id,
						user_city_id=$user_city_id,
						lang_code='$lang_code'";
		$this->execQuery($qs);
		

		$qs = "INSERT DELAYED 
						".$this->getDbName().".tmp_session
					SET 
						user_id=$user_id,
						partner_id=".$_SESSION['partner_id'].",
						partner_host_name='".$_SESSION['partner_host_name']."',
						lang_id=".$_SESSION['lang_id'].",
						partner_ip=".$_SESSION['partner_id'];
		$this->execQuery($qs);
		
		$http_host = $this->checkTextForMysql($_SERVER['HTTP_HOST']);
		$http_x_real_ip = $_SERVER['HTTP_X_REAL_IP'];
		$http_x_forwarded_for = $_SERVER['HTTP_X_FORWARDED_FOR'];
		$http_user_agent = $this->checkTextForMysql($_SERVER['HTTP_USER_AGENT']);
		$http_referer = $this->checkTextForMysql($_SERVER['HTTP_REFERER']);
		
		if ($http_referer)
		{
			preg_match("/^(http:\/\/)?([^\/]+)/i",$http_referer, $matches);
			$http_referer_host = $matches[2];
		}	
		
		$http_from = $this->checkTextForMysql($_SERVER['HTTP_FROM']);
		$http_cookie = $this->checkTextForMysql($_SERVER['HTTP_COOKIE']);
		$redirect_query_string = $this->checkTextForMysql($_SERVER['REDIRECT_QUERY_STRING']);
		$request_uri = $this->checkTextForMysql($_SERVER['REQUEST_URI']);
		$script_name = $this->checkTextForMysql($_SERVER['SCRIPT_NAME']);
		$query_string = $this->checkTextForMysql($_SERVER['QUERY_STRING']);
		$reques_time = $_SERVER['REQUEST_TIME']*1;
		
		
		$qs = "INSERT DELAYED 
						".$this->getDbName().".tmp_server
					SET 
						user_id=$user_id,
						HTTP_HOST='$http_host',
						HTTP_X_REAL_IP='$http_x_real_ip',
						HTTP_X_FORWARDED_FOR='$http_x_forwarded_for',
						HTTP_USER_AGENT='$http_user_agent',
						HTTP_REFERER='$http_referer',
						HTTP_REFERER_HOST='$http_referer_host',
						HTTP_FROM='$http_from',
						HTTP_COOKIE='$http_cookie',
						REDIRECT_QUERY_STRING='$redirect_query_string',
						REQUEST_URI='$request_uri',
						SCRIPT_NAME='$script_name',
						QUERY_STRING='$query_string',
						REQUEST_TIME=$reques_time,
						is_bot=$is_bot";
		$this->execQuery($qs);
	}
}
?>